package com.Studen.controller;



import com.Studen.Dao.TeacherLoginDaoMpl;
import com.Studen.bean.LoginFile;
import com.Studen.servlet.TeacherLoginSerMpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/TeacherLogin")
public class TeacherLogin extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /*
           首先设置乱码
        *  1、获取注册界面传来的数据、并对其进行封装成LoginData对象
        *     用Integer.parseInt()函数将性别转换为int类型 1为男生 0为女生
        *  2、判断是否注册成功：
        *       如果成功跳转到登录页面、并提示注册成功
        *       如果失败跳转到注册页面重新注册、并给出注册失败的原因
        *       判断：0、账户为空
        *            1、账户为空
        *            2、两次的密码不一致
        *            3、注册成功
        *
        * */

        request.setCharacterEncoding("UTF-8");
        String name=request.getParameter("name");
        String pwd=request.getParameter("pwd");
        String pwd_s=request.getParameter("pwd_s");
        int sex= Integer.parseInt(request.getParameter("sex"));

        LoginFile data = new LoginFile();
        data.setName(name);
        data.setPwd(pwd);
        data.setPwd_s(pwd_s);
        data.setSex(sex);
        new TeacherLoginDaoMpl().Login_test(data);
        int i = new TeacherLoginSerMpl().loginData(data);
        if (i==0){
            request.setAttribute("ts","账户为空");
            request.getRequestDispatcher("TeacherLogin.jsp").forward(request,response);
        }
        if (i==1){
            request.setAttribute("ts","密码为空");
            request.getRequestDispatcher("TeacherLogin.jsp").forward(request,response);
        }
        if (i==2){
            request.setAttribute("ts","两次输入的密码不一致");
            request.getRequestDispatcher("TeacherLogin.jsp").forward(request,response);
        }
        if (i==3)
        {
            request.setAttribute("ts","注册成功");
            request.getRequestDispatcher("login.jsp").forward(request,response);
        }
    }
}
